

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Blue~u~u~u">
  <meta name="author" content="Blue~u~u~u">
  <meta name="keywords" content="">
  <meta name="description" content="spring-day02第一章 基于注解管理bean第一节 IOC的相关注解及包扫描1. 注解的优势和 XML 配置文件一样，注解本身并不能执行，注解本身仅仅只是做一个标记，具体的功能是框架检测到注解标记的位置，然后针对这个位置按照注解标记的功能来执行具体操作。使用注解开发比使用XML更加简洁明了 2. 包扫描使用注解进行Bean管理必须要在xml配置文件中进行包扫描，这样的话spring-fra">
<meta property="og:type" content="article">
<meta property="og:title" content="spring-day02-annotation">
<meta property="og:url" content="http://www.slx.blue/2021/12/06/spring-day02/index.html">
<meta property="og:site_name" content="Blue~u~u~u~u">
<meta property="og:description" content="spring-day02第一章 基于注解管理bean第一节 IOC的相关注解及包扫描1. 注解的优势和 XML 配置文件一样，注解本身并不能执行，注解本身仅仅只是做一个标记，具体的功能是框架检测到注解标记的位置，然后针对这个位置按照注解标记的功能来执行具体操作。使用注解开发比使用XML更加简洁明了 2. 包扫描使用注解进行Bean管理必须要在xml配置文件中进行包扫描，这样的话spring-fra">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_001.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_002.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_003.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_004.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_005.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_006.png">
<meta property="og:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_007.png">
<meta property="article:published_time" content="2021-12-06T06:35:17.000Z">
<meta property="article:modified_time" content="2021-12-06T06:37:55.429Z">
<meta property="article:author" content="Blue~u~u~u">
<meta property="article:tag" content="spring">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://www.slx.blue/2021/12/06/spring-day02/tu_001.png">
  
  <title>spring-day02-annotation - Blue~u~u~u~u</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.slx.blue","root":"/","version":"1.8.12","typing":{"enable":true,"typeSpeed":180,"cursorChar":"_","loop":true},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname"}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"/>
<script src="/live2d-widget/autoload.js"></script>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Blue~u~u</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/images/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="spring-day02-annotation">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-12-06 14:35" pubdate>
        2021年12月6日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      15k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      46 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">spring-day02-annotation</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：6 个月前
                
              </p>
            
            <div class="markdown-body">
              <h1 id="spring-day02"><a href="#spring-day02" class="headerlink" title="spring-day02"></a>spring-day02</h1><h2 id="第一章-基于注解管理bean"><a href="#第一章-基于注解管理bean" class="headerlink" title="第一章 基于注解管理bean"></a>第一章 基于注解管理bean</h2><h3 id="第一节-IOC的相关注解及包扫描"><a href="#第一节-IOC的相关注解及包扫描" class="headerlink" title="第一节 IOC的相关注解及包扫描"></a>第一节 IOC的相关注解及包扫描</h3><h4 id="1-注解的优势"><a href="#1-注解的优势" class="headerlink" title="1. 注解的优势"></a>1. 注解的优势</h4><p>和 XML 配置文件一样，注解本身并不能执行，注解本身仅仅只是做一个标记，具体的功能是框架检测到注解标记的位置，然后针对这个位置按照注解标记的功能来执行具体操作。使用注解开发比使用XML更加简洁明了</p>
<h4 id="2-包扫描"><a href="#2-包扫描" class="headerlink" title="2. 包扫描"></a>2. 包扫描</h4><p>使用注解进行Bean管理必须要在xml配置文件中进行包扫描，这样的话spring-framework才能够解析你在Bean上所添加的注解，包扫描包含下面列举的四种情况:</p>
<h5 id="2-1-最基本的扫描方式-常用"><a href="#2-1-最基本的扫描方式-常用" class="headerlink" title="2.1 最基本的扫描方式[常用]"></a>2.1 最基本的扫描方式[常用]</h5><p>扫描指定包下的所有类以及其子包下的所有类</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 配置自动扫描的包 --&gt;</span><br><span class="hljs-comment">&lt;!-- 最基本的扫描方式 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-2-指定匹配模式"><a href="#2-2-指定匹配模式" class="headerlink" title="2.2 指定匹配模式"></a>2.2 指定匹配模式</h5><p>根据具体的匹配规则，扫描某个包下的某些类，需要注意规则匹配是匹配的直接在这个包下的类，不能是子包下的类</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 情况二：在指定扫描包的基础上指定匹配模式 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span></span><br><span class="hljs-tag">                        <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component&quot;</span></span><br><span class="hljs-tag">                        <span class="hljs-attr">resource-pattern</span>=<span class="hljs-string">&quot;Soldier*.class&quot;</span>/&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-3-指定要排除的组件"><a href="#2-3-指定要排除的组件" class="headerlink" title="2.3 指定要排除的组件"></a>2.3 指定要排除的组件</h5><p>扫描某个包下的所有类，但是排除掉一些注解</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 情况三：指定不扫描的组件 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component&quot;</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- context:exclude-filter标签：指定排除规则 --&gt;</span><br>    <span class="hljs-comment">&lt;!-- type属性：指定根据什么来进行排除，annotation取值表示根据注解来排除 --&gt;</span><br>    <span class="hljs-comment">&lt;!-- expression属性：指定排除规则的表达式，对于注解来说指定全类名即可 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">context:exclude-filter</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;annotation&quot;</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">&quot;org.springframework.stereotype.Controller&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">context:component-scan</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-4-仅扫描指定组件"><a href="#2-4-仅扫描指定组件" class="headerlink" title="2.4 仅扫描指定组件"></a>2.4 仅扫描指定组件</h5><p>扫描某个包下的所有类，但是只扫描某种注解</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!-- 情况四：仅扫描指定的组件 --&gt;</span><br><span class="hljs-comment">&lt;!-- 仅扫描 = 关闭默认规则 + 追加规则 --&gt;</span><br><span class="hljs-comment">&lt;!-- use-default-filters属性：取值false表示关闭默认扫描规则 --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">context:component-scan</span> <span class="hljs-attr">base-package</span>=<span class="hljs-string">&quot;com.atguigu.ioc.component&quot;</span> <span class="hljs-attr">use-default-filters</span>=<span class="hljs-string">&quot;false&quot;</span>&gt;</span><br>    <br>    <span class="hljs-comment">&lt;!-- context:include-filter标签：指定在原有扫描规则的基础上追加的规则 --&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">context:include-filter</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;annotation&quot;</span> <span class="hljs-attr">expression</span>=<span class="hljs-string">&quot;org.springframework.stereotype.Controller&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">context:component-scan</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h4 id="3-常用的进行IOC的注解"><a href="#3-常用的进行IOC的注解" class="headerlink" title="3. 常用的进行IOC的注解"></a>3. 常用的进行IOC的注解</h4><p>我们在类上添加注解，可以实现将该类的对象配置到spring的IOC容器中，常用的注解有如下四种:</p>
<h5 id="3-1-Component注解"><a href="#3-1-Component注解" class="headerlink" title="3.1 Component注解"></a>3.1 Component注解</h5><p>该注解主要用在普通类上，即除了三层结构之外的其它类的对象如果需要配置到spring的IOC容器中，那么则需要在类上添加Component注解</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><span class="hljs-meta">@Component</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CommonComponent</span> </span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-2-Controller注解"><a href="#3-2-Controller注解" class="headerlink" title="3.2 Controller注解"></a>3.2 Controller注解</h5><p>该注解主要用在控制层的类上，控制器处于三层结构中的表现层，在JavaWeb阶段表现层使用的是Servlet，而在学习了spring-framework之后，表现层我们使用Controller代替</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;<br><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierController</span> </span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-3-Service注解"><a href="#3-3-Service注解" class="headerlink" title="3.3 Service注解"></a>3.3 Service注解</h5><p>该注解主要用在三层结构中的业务层的实现类上，用于将业务层的对象配置到spring的IOC容器中</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Service;<br><span class="hljs-meta">@Service</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierService</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ISoldierService</span></span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="3-4-Repository注解"><a href="#3-4-Repository注解" class="headerlink" title="3.4 Repository注解"></a>3.4 Repository注解</h5><p>该注解主要用在三层结构中的持久层的实现类上，用于将持久层的对象配置到spring的IOC容器中，但是以后我们整合了Mybatis，这里就变成了Mapper接口，而Mapper接口是由Mybatis和Spring的整合包负责扫描的。由于Mybatis整合包想要把Mapper接口背后的代理类加入Spring的IOC容器需要结合Mybatis对Mapper配置文件的解析，所以这个事情是Mybatis和Spring的整合包来完成，将来由Mybatis负责扫描，也不需要使用Repository注解。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;<br><span class="hljs-meta">@Repository</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierDao</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">ISoldierDao</span></span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>虽然我们学习的上述四个注解都实现IOC，但是其实他们四个在本质上是没有区别的。通过查看源码我们得知，@Controller、@Service、@Repository这三个注解只是在@Component注解的基础上起了三个新的名字。对于Spring使用IOC容器管理这些组件来说没有区别。所以@Controller、@Service、@Repository这三个注解只是给开发人员看的，让我们能够便于分辨组件的作用。</p>
<p>注意：虽然它们本质上一样，但是为了代码的可读性，为了程序结构严谨我们肯定不能随便胡乱标记。</p>
<p><img src="http://www.slx.blue/2021/12/06/spring-day02/tu_001.png" srcset="/img/loading.gif" lazyload></p>
<h4 id="4-给Bean设置name"><a href="#4-给Bean设置name" class="headerlink" title="4. 给Bean设置name"></a>4. 给Bean设置name</h4><p>在我们使用XML方式管理bean的时候，每个bean都有一个唯一标识，便于在其他地方引用。现在使用注解后，每个组件仍然应该有一个唯一标识。 </p>
<h5 id="4-1-默认情况"><a href="#4-1-默认情况" class="headerlink" title="4.1 默认情况"></a>4.1 默认情况</h5><p>类名首字母小写就是bean的id。例如：SoldierController类对应的bean的id就是soldierController。 </p>
<h5 id="4-2-使用value属性指定"><a href="#4-2-使用value属性指定" class="headerlink" title="4.2 使用value属性指定"></a>4.2 使用value属性指定</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller(value = &quot;tianDog&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierController</span> </span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>当注解中只设置一个属性时，value属性的属性名可以省略： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller(&quot;tianDog&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierController</span> </span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="5-怎么选择"><a href="#5-怎么选择" class="headerlink" title="5. 怎么选择"></a>5. 怎么选择</h4><ol>
<li>如果这个类是你自己写的类，表示你可以在它上面加注解，所以可以使用注解方式进行IOC</li>
<li>如果这个类不是你自己写的类，而是第三方依赖中的类，表示你不能在它上面加注解，那么就只能用配置文件方式进行IOC</li>
</ol>
<h3 id="第二节-依赖注入相关的注解"><a href="#第二节-依赖注入相关的注解" class="headerlink" title="第二节 依赖注入相关的注解"></a>第二节 依赖注入相关的注解</h3><h4 id="2-1-注入简单类型的属性"><a href="#2-1-注入简单类型的属性" class="headerlink" title="2.1 注入简单类型的属性"></a>2.1 注入简单类型的属性</h4><p>Value注解是用于给IOC容器中的Bean注入简单类型的属性值</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Service(&quot;smallDog&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierService</span> </span>&#123;<br>    <span class="hljs-meta">@Value(&quot;aobama&quot;)</span><br>    <span class="hljs-keyword">private</span> String name;<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="2-2-读取properties中的数据并且进行注入"><a href="#2-2-读取properties中的数据并且进行注入" class="headerlink" title="2.2 读取properties中的数据并且进行注入"></a>2.2 读取properties中的数据并且进行注入</h4><h5 id="2-2-1-准备properties配置文件"><a href="#2-2-1-准备properties配置文件" class="headerlink" title="2.2.1 准备properties配置文件"></a>2.2.1 准备properties配置文件</h5><p>happyInfo.properties</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs properties"><span class="hljs-meta">happy.componentName</span>=<span class="hljs-string">aoalfu</span><br></code></pre></td></tr></table></figure>

<h5 id="2-2-2-使用PropertySource注解读取配置文件数据"><a href="#2-2-2-使用PropertySource注解读取配置文件数据" class="headerlink" title="2.2.2 使用PropertySource注解读取配置文件数据"></a>2.2.2 使用PropertySource注解读取配置文件数据</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.PropertySource;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 当注解中只有一个属性需要赋值，并且这个属性的名字叫&quot;value&quot;的时候，可以省略&quot;value = &quot;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Component(&quot;happyComponent&quot;)</span><br><span class="hljs-meta">@PropertySource(&quot;classpath:happyInfo.properties&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-meta">@Value(&quot;$&#123;happy.componentName&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String componentName;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getComponentName</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> componentName;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="2-3-注入Bean类型属性"><a href="#2-3-注入Bean类型属性" class="headerlink" title="2.3 注入Bean类型属性"></a>2.3 注入Bean类型属性</h4><h5 id="2-3-1-设定情景"><a href="#2-3-1-设定情景" class="headerlink" title="2.3.1 设定情景"></a>2.3.1 设定情景</h5><ul>
<li>UserController需要UserService</li>
<li>UserService需要UserDao</li>
<li>UserDao需要HappyComponent</li>
<li>HappyComponent需要componentName</li>
</ul>
<p>同时在各个组件中声明要调用的方法。 </p>
<h5 id="2-3-2-在各个组件中声明成员变量和方法"><a href="#2-3-2-在各个组件中声明成员变量和方法" class="headerlink" title="2.3.2 在各个组件中声明成员变量和方法"></a>2.3.2 在各个组件中声明成员变量和方法</h5><h6 id="2-3-2-1-UserController"><a href="#2-3-2-1-UserController" class="headerlink" title="2.3.2.1 UserController"></a>2.3.2.1 UserController</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.controller;<br><br><span class="hljs-keyword">import</span> com.atguigu.service.UserService;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 为了规范代码，提高代码的可读性，三层结构中不同层次的类使用不同的IOC注解</span><br><span class="hljs-comment"> * 1. Controller注解使用在表现层</span><br><span class="hljs-comment"> * 2. Service注解使用在业务层</span><br><span class="hljs-comment"> * 3. Repository注解使用在持久层</span><br><span class="hljs-comment"> * 4. Component注解使用在三层结构之外的类上</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * 依赖注入的注解:</span><br><span class="hljs-comment"> * 1. Autowired注解: 表示自动装配,它只能用于注入Bean类型的对象</span><br><span class="hljs-comment"> * 2. Value注解: 注入简单类型的值</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Controller(&quot;userController&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> UserService userService;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printName</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;打印:&quot;</span> + userService.getName());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-3-2-2-UserServiceImpl"><a href="#2-3-2-2-UserServiceImpl" class="headerlink" title="2.3.2.2 UserServiceImpl"></a>2.3.2.2 UserServiceImpl</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.service.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.dao.UserDao;<br><span class="hljs-keyword">import</span> com.atguigu.service.UserService;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Service;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.service.impl</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Service(&quot;userService&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserServiceImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserService</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> UserDao userDao;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> userDao.getName();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-3-2-3-UserDaoImpl"><a href="#2-3-2-3-UserDaoImpl" class="headerlink" title="2.3.2.3 UserDaoImpl"></a>2.3.2.3 UserDaoImpl</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.dao.impl;<br><br><span class="hljs-keyword">import</span> com.atguigu.component.HappyComponent;<br><span class="hljs-keyword">import</span> com.atguigu.dao.UserDao;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Repository;<br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.dao.impl</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Repository(&quot;userDao&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserDaoImpl</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">UserDao</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> HappyComponent happyComponent;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-comment">//按道理来说要执行SQL语句到数据查询数据</span><br>        <span class="hljs-comment">//但是今天通过依赖注入，模拟从数据库查询到name</span><br>        <span class="hljs-keyword">return</span> happyComponent.getComponentName();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-3-2-4-HappyComponent"><a href="#2-3-2-4-HappyComponent" class="headerlink" title="2.3.2.4 HappyComponent"></a>2.3.2.4 HappyComponent</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.component;<br><br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Component;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 当注解中只有一个属性需要赋值，并且这个属性的名字叫&quot;value&quot;的时候，可以省略&quot;value = &quot;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Component(&quot;happyComponent&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HappyComponent</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> String componentName;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getComponentName</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> componentName;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="2-3-3-使用Autowired注解进行注入"><a href="#2-3-3-使用Autowired注解进行注入" class="headerlink" title="2.3.3 使用Autowired注解进行注入"></a>2.3.3 使用Autowired注解进行注入</h5><h6 id="2-3-3-1-Autowired注解可以使用的位置"><a href="#2-3-3-1-Autowired注解可以使用的位置" class="headerlink" title="2.3.3.1 Autowired注解可以使用的位置"></a>2.3.3.1 Autowired注解可以使用的位置</h6><ol>
<li><p>用在成员变量上(最常用)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.controller;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;<br><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> UserService userService;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">printName</span><span class="hljs-params">()</span></span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;打印:&quot;</span> + userService.getName());<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>用在构造器上</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> UserService userService;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">UserController</span><span class="hljs-params">(UserService userService)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.userService = userService;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>用在方法上</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> UserService userService;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserService</span><span class="hljs-params">((UserService userService)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.userService = userService;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure></li>
<li><p>用在参数上</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">UserController</span> </span>&#123;<br>    <span class="hljs-keyword">private</span> UserService userService;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">UserController</span><span class="hljs-params">(<span class="hljs-meta">@Autowired</span> UserService userService)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.userService = userService;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h6 id="2-3-3-2-Autowired工作流程"><a href="#2-3-3-2-Autowired工作流程" class="headerlink" title="2.3.3.2 Autowired工作流程"></a>2.3.3.2 Autowired工作流程</h6><p><img src="http://www.slx.blue/2021/12/06/spring-day02/tu_002.png" srcset="/img/loading.gif" lazyload></p>
</li>
</ol>
<p>首先根据所需要的组件类型到IOC容器中查找</p>
<ul>
<li>能够找到唯一的bean：直接执行装配</li>
<li>如果完全找不到匹配这个类型的bean：装配失败</li>
<li>和所需类型匹配的bean不止一个<ul>
<li>没有@Qualifier注解：根据@Autowired标记位置成员变量的变量名作为bean的id进行匹配<ul>
<li>能够找到：执行装配</li>
<li>找不到：装配失败</li>
</ul>
</li>
<li>使用@Qualifier注解：根据@Qualifier注解中指定的名称作为bean的id进行匹配<ul>
<li>能够找到：执行装配</li>
<li>找不到：装配失败</li>
</ul>
</li>
</ul>
</li>
</ul>
<h5 id="2-3-4-使用Resource注解进行注入"><a href="#2-3-4-使用Resource注解进行注入" class="headerlink" title="2.3.4 使用Resource注解进行注入"></a>2.3.4 使用Resource注解进行注入</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.ioc.component;<br><span class="hljs-keyword">import</span> org.springframework.stereotype.Controller;<br><span class="hljs-meta">@Controller</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SoldierController</span> </span>&#123;<br>    <span class="hljs-meta">@Resource</span><br>    <span class="hljs-keyword">private</span> SoldierService soldierService;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getMessage</span><span class="hljs-params">()</span> </span>&#123;<br>        soldierService.getMessage();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="2-3-5-Autowired注解和Resource注解的区别"><a href="#2-3-5-Autowired注解和Resource注解的区别" class="headerlink" title="2.3.5 Autowired注解和Resource注解的区别"></a>2.3.5 Autowired注解和Resource注解的区别</h5><p>@Autowired功能虽说非常强大，但是也有些不足之处。比如：比如它跟spring强耦合了，如果换成了JFinal等其他框架，功能就会失效。而@Resource是JSR-250提供的，它是Java标准，绝大部分框架都支持。</p>
<p>除此之外，有些场景使用@Autowired无法满足的要求，改成@Resource却能解决问题。接下来，我们重点看看@Autowired和@Resource的区别。</p>
<ul>
<li>@Autowired默认按byType自动装配，而@Resource默认byName自动装配。</li>
<li>@Autowired只包含一个参数：required，表示是否开启自动注入，默认是true。而@Resource包含七个参数，其中最重要的两个参数是：name 和 type。</li>
<li>@Autowired如果要使用byName，需要使用@Qualifier一起配合。而@Resource如果指定了name，则用byName自动装配，如果指定了type，则用byType自动装配。</li>
<li>@Autowired能够用在：构造器、方法、参数、成员变量和注解上，而@Resource能用在：类、成员变量和方法上。</li>
<li>@Autowired是spring定义的注解，而@Resource是JSR-250定义的注解。</li>
</ul>
<p>此外，它们的装配顺序不同。 </p>
<p>Autowired的装配顺序:</p>
<img src="http://www.slx.blue/2021/12/06/spring-day02/tu_003.png" srcset="/img/loading.gif" lazyload style="zoom:67%;">

<p>Resource的装配顺序:</p>
<ol>
<li>如果同时指定了name和type： </li>
</ol>
<img src="http://www.slx.blue/2021/12/06/spring-day02/tu_004.png" srcset="/img/loading.gif" lazyload style="zoom:50%;">

<ol start="2">
<li> 如果指定了name： </li>
</ol>
<img src="http://www.slx.blue/2021/12/06/spring-day02/tu_005.png" srcset="/img/loading.gif" lazyload style="zoom:50%;">

<ol start="3">
<li>如果指定了type: </li>
</ol>
<img src="http://www.slx.blue/2021/12/06/spring-day02/tu_006.png" srcset="/img/loading.gif" lazyload style="zoom:50%;">

<ol start="4">
<li> 如果既没有指定name，也没有指定type： </li>
</ol>
<img src="http://www.slx.blue/2021/12/06/spring-day02/tu_007.png" srcset="/img/loading.gif" lazyload style="zoom:67%;">

<h3 id="第三节-纯注解开发"><a href="#第三节-纯注解开发" class="headerlink" title="第三节 纯注解开发"></a>第三节 纯注解开发</h3><p>体验纯注解开发，是为了给将来学习SpringBoot打基础。因为在SpringBoot中，就是完全舍弃XML配置文件，全面使用注解来完成主要的配置。 </p>
<h4 id="1-使用配置类取代配置文件"><a href="#1-使用配置类取代配置文件" class="headerlink" title="1. 使用配置类取代配置文件"></a>1. 使用配置类取代配置文件</h4><h5 id="1-1-创建配置类"><a href="#1-1-创建配置类" class="headerlink" title="1.1 创建配置类"></a>1.1 创建配置类</h5><p>使用@Configuration注解将一个普通的类标记为Spring的配置类。 </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.configuration;<br><br><span class="hljs-keyword">import</span> com.alibaba.druid.pool.DruidDataSource;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.ComponentScan;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.PropertySource;<br><br><span class="hljs-keyword">import</span> javax.sql.DataSource;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.configuration</span><br><span class="hljs-comment"> * 1. 配置类上要添加一个<span class="hljs-doctag">@Configuration</span>注解</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AtguiguSpringConfiguration</span> </span>&#123;<br>    <br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-2-在配置类中配置包扫描"><a href="#1-2-在配置类中配置包扫描" class="headerlink" title="1.2 在配置类中配置包扫描"></a>1.2 在配置类中配置包扫描</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.configuration;<br><br><span class="hljs-keyword">import</span> com.alibaba.druid.pool.DruidDataSource;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.ComponentScan;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.PropertySource;<br><br><span class="hljs-keyword">import</span> javax.sql.DataSource;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.configuration</span><br><span class="hljs-comment"> * 1. 配置类上要添加一个<span class="hljs-doctag">@Configuration</span>注解</span><br><span class="hljs-comment"> * 2. 使用<span class="hljs-doctag">@ComponentScan</span>注解进行包扫描</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-meta">@ComponentScan(&quot;com.atguigu&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AtguiguSpringConfiguration</span> </span>&#123;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-3-在配置类中配置bean"><a href="#1-3-在配置类中配置bean" class="headerlink" title="1.3 在配置类中配置bean"></a>1.3 在配置类中配置bean</h5><p>对Bean进行IOC的时候，如果是自己编写的类，则可以直接通过IOC注解进行配置，如果是<strong>非自己写的类</strong>:例如JDK中或者第三方框架中的类，我们可以通过配置文件进行IOC；但是在纯注解中没有了配置文件，所以我们需要使用@Bean注解进行IOC</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu.configuration;<br><br><span class="hljs-keyword">import</span> com.alibaba.druid.pool.DruidDataSource;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Bean;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.ComponentScan;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.Configuration;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.PropertySource;<br><br><span class="hljs-keyword">import</span> javax.sql.DataSource;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu.configuration</span><br><span class="hljs-comment"> * 1. 配置类上要添加一个<span class="hljs-doctag">@Configuration</span>注解</span><br><span class="hljs-comment"> * 2. 使用<span class="hljs-doctag">@ComponentScan</span>注解进行包扫描</span><br><span class="hljs-comment"> * 3. 使用<span class="hljs-doctag">@Bean</span>注解配置第三方的类的IOC</span><br><span class="hljs-comment"> * 4. 从properties文件中读取数据</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@Configuration</span><br><span class="hljs-meta">@ComponentScan(&quot;com.atguigu&quot;)</span><br><span class="hljs-meta">@PropertySource(&quot;classpath:jdbcInfo.properties&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AtguiguSpringConfiguration</span> </span>&#123;<br>    <span class="hljs-meta">@Value(&quot;$&#123;jdbc.username&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String username;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;jdbc.password&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String password;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;jdbc.url&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String url;<br><br>    <span class="hljs-meta">@Value(&quot;$&#123;jdbc.driver&#125;&quot;)</span><br>    <span class="hljs-keyword">private</span> String driver;<br>    <span class="hljs-meta">@Bean</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> DataSource <span class="hljs-title">getDataSource</span><span class="hljs-params">()</span></span>&#123;<br>        DruidDataSource dataSource = <span class="hljs-keyword">new</span> DruidDataSource();<br>        dataSource.setDriverClassName(driver);<br>        dataSource.setUrl(url);<br>        dataSource.setUsername(username);<br>        dataSource.setPassword(password);<br>        <span class="hljs-keyword">return</span> dataSource;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h5 id="1-4-根据配置类创建IOC容器对象"><a href="#1-4-根据配置类创建IOC容器对象" class="headerlink" title="1.4 根据配置类创建IOC容器对象"></a>1.4 根据配置类创建IOC容器对象</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// AnnotationConfigApplicationContext根据配置类创建IOC容器对象</span><br>ApplicationContext iocContainerAnnotation = <span class="hljs-keyword">new</span> AnnotationConfigApplicationContext(MyConfiguration.class);<br></code></pre></td></tr></table></figure>

<h5 id="1-5-测试"><a href="#1-5-测试" class="headerlink" title="1.5 测试"></a>1.5 测试</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu;<br><br><span class="hljs-keyword">import</span> com.atguigu.configuration.AtguiguSpringConfiguration;<br><span class="hljs-keyword">import</span> com.atguigu.controller.UserController;<br><span class="hljs-keyword">import</span> org.junit.Test;<br><span class="hljs-keyword">import</span> org.springframework.context.ApplicationContext;<br><span class="hljs-keyword">import</span> org.springframework.context.annotation.AnnotationConfigApplicationContext;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu</span><br><span class="hljs-comment"> */</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestAnnotationOnly</span> </span>&#123;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testPrintConnection</span><span class="hljs-params">()</span></span>&#123;<br>        <span class="hljs-comment">//1. 创建核心容器对象:AnnotationConfigApplicationContext核心容器是加载配置类的核心容器</span><br>        ApplicationContext act = <span class="hljs-keyword">new</span> AnnotationConfigApplicationContext(AtguiguSpringConfiguration.class);<br>        <span class="hljs-comment">//2. 从核心容器对象中获取UserController对象</span><br>        UserController userController = (UserController) act.getBean(<span class="hljs-string">&quot;userController&quot;</span>);<br>        <span class="hljs-comment">//3. 调用UserController对象printConnection()方法</span><br>        userController.printConnection();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第四节-Spring整合junit4"><a href="#第四节-Spring整合junit4" class="headerlink" title="第四节 Spring整合junit4"></a>第四节 Spring整合junit4</h3><h4 id="1-Spring整合Junit4的好处"><a href="#1-Spring整合Junit4的好处" class="headerlink" title="1. Spring整合Junit4的好处"></a>1. Spring整合Junit4的好处</h4><ul>
<li>好处1：不需要自己创建IOC容器对象了</li>
<li>好处2：任何需要的bean都可以在测试类中直接享受自动装配</li>
</ul>
<h4 id="2-具体操作"><a href="#2-具体操作" class="headerlink" title="2. 具体操作"></a>2. 具体操作</h4><h5 id="2-1-加入依赖"><a href="#2-1-加入依赖" class="headerlink" title="2.1 加入依赖"></a>2.1 加入依赖</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-comment">&lt;!--</span><br><span class="hljs-comment">   引入Spring整合Junit的依赖</span><br><span class="hljs-comment"> --&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.springframework<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-test<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>5.3.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">scope</span>&gt;</span>test<span class="hljs-tag">&lt;/<span class="hljs-name">scope</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br></code></pre></td></tr></table></figure>

<h5 id="2-2-创建测试类"><a href="#2-2-创建测试类" class="headerlink" title="2.2 创建测试类"></a>2.2 创建测试类</h5><p>如果是使用的配置类:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">package</span> com.atguigu;<br><br><span class="hljs-keyword">import</span> com.atguigu.configuration.AtguiguSpringConfiguration;<br><span class="hljs-keyword">import</span> com.atguigu.controller.UserController;<br><span class="hljs-keyword">import</span> org.junit.Test;<br><span class="hljs-keyword">import</span> org.junit.runner.RunWith;<br><span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Autowired;<br><span class="hljs-keyword">import</span> org.springframework.test.context.ContextConfiguration;<br><span class="hljs-keyword">import</span> org.springframework.test.context.junit4.SpringJUnit4ClassRunner;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 包名:com.atguigu</span><br><span class="hljs-comment"> * 目标:在单元测试类中注入要测试的对象,这样就不需要自己创建核心容器了，也不需要自己调用getBean()方法从核心容器中获取对象了</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> * 实现方案:使用Spring整合Junit单元测试</span><br><span class="hljs-comment"> * 实现步骤:</span><br><span class="hljs-comment"> * 1. 引入spring整合Junit的依赖</span><br><span class="hljs-comment"> * 2. 让单元测试类依赖SpringJUnit4ClassRunner来运行:在测试类上添加			  <span class="hljs-doctag">@RunWith</span>(SpringJUnit4ClassRunner.class)</span><br><span class="hljs-comment"> * 3. 加载配置文件或者配置类: 在测试类上添加<span class="hljs-doctag">@ContextConfiguration</span>(classes = AtguiguSpringConfiguration.class)		</span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">@RunWith(SpringJUnit4ClassRunner.class)</span><br><span class="hljs-meta">@ContextConfiguration(classes = AtguiguSpringConfiguration.class)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestAnnotationOnly</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> UserController userController;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testPrintConnection</span><span class="hljs-params">()</span></span>&#123;<br>        <span class="hljs-comment">//3. 调用UserController对象printConnection()方法</span><br>        userController.printConnection();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>如果是使用的配置文件:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-meta">@RunWith(SpringJUnit4ClassRunner.class)</span><br><span class="hljs-meta">@ContextConfiguration(&quot;classpath:spring-application.xml&quot;)</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TestSpringAnnotation</span> </span>&#123;<br>    <span class="hljs-meta">@Autowired</span><br>    <span class="hljs-keyword">private</span> UserController userController;<br>    <span class="hljs-meta">@Test</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">testPrintConnection</span><span class="hljs-params">()</span></span>&#123;<br>        <span class="hljs-comment">//3. 调用UserController对象printConnection()方法</span><br>        userController.printConnection();<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h3 id="第五节-内容总结"><a href="#第五节-内容总结" class="headerlink" title="第五节 内容总结"></a>第五节 内容总结</h3><ol>
<li>包扫描: 指定要进行组件扫描的包</li>
<li>IOC 注解:<ol>
<li>Component注解: 三层结构之外的其他类使用</li>
<li>Controller注解: 表现层的类使用</li>
<li>Service注解: 业务层的类使用</li>
<li>Repository注解: 持久层的类使用</li>
</ol>
</li>
<li>依赖注入注解：<ol>
<li>注入Bean:<ol>
<li>Autowired注解，它是byType进行自动注入，如果要byName必须结合Qualifier注解一起使用</li>
<li>Resource注解，它默认是byName进行自动注入</li>
</ol>
</li>
<li>注入简单类型: Value注解，以及使用PropertySource注解引入外部的properties文件</li>
</ol>
</li>
<li>注解方式和配置文件方式进行IOC和依赖注入的选择问题:<ol>
<li>如果是自己写的类就使用注解方式</li>
<li>如果是第三方jar中的类就使用配置文件方式</li>
</ol>
</li>
<li>Spring整合Junit：<ol>
<li>目的：简化单元测试</li>
<li>实现步骤:<ol>
<li>引入spring-test的依赖</li>
<li>Junit的依赖版本必须是4.12及以上</li>
<li>给单元测试类添加@Runwith(SpringJUnit4ClassRunner.class)</li>
<li>给单元测试类添加@ContextConfiguration(locations=”配置文件的路径”或者是classes=配置类.class)</li>
<li>直接注入你想使用的IOC容器中的对象就可以直接使用了</li>
</ol>
</li>
</ol>
</li>
<li>Spring的纯注解开发<ol>
<li>目的: 为了以后学习SpringBoot做准备，我们项目如果是使用Spring做开发的话是不会用纯注解的</li>
<li>步骤:<ol>
<li>配置类上要添加@Configuration注解标示为配置类</li>
<li>配置类上要添加@ComponentScan指定要扫描的包</li>
<li>如果要对第三方的类进行IOC配置<ol>
<li>在配置类中创建一个方法<ol>
<li>修饰符public</li>
<li>返回值是要进行IOC的对象的类型</li>
<li>方法体中编写创建IOC对象的代码</li>
<li>如果要给这个方法注入一个IOC容器中存在的对象，直接在方法的参数中声明就行了</li>
</ol>
</li>
<li>给该方法添加@Bean注解</li>
</ol>
</li>
<li>如果是使用纯注解开发，整合Junit的时候，@ContextConfiguration(classes=配置类.class)</li>
</ol>
</li>
</ol>
</li>
</ol>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/spring/">spring</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/spring/">spring</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客目前大部分文章都是参考尚硅谷或者马士兵教育的学习资料！<a href="http://www.atguigu.com/" rel="nofollow noopener"官网地址！</a> 
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/06/java%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F-%E4%B8%83%E5%A4%A7%E5%8E%9F%E5%88%99/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">java设计模式-七大原则</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/06/%E5%AD%97%E7%AC%A6%E9%9B%86%E5%92%8C%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98/">
                        <span class="hidden-mobile">字符集和编码问题</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                

              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">

  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
	<!--《添加网站运行时间 -->
<br/>

<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
var now = new Date(); 

function createtime() {
    //此处修改你的建站时间或者网站上线时间
    var grt = new Date('11/02/2021 21:39:00');
    now.setTime(now.getTime() + 250);
    days = (now - grt) / 1000 / 60 / 60 / 24;

    dnum = Math.floor(days);
    hours = (now - grt) / 1000 / 60 / 60 - (24 * dnum);
    hnum = Math.floor(hours);
    if (String(hnum).length == 1) {
        hnum = "0" + hnum;
    }
    minutes = (now - grt) / 1000 / 60 - (24 * 60 * dnum) - (60 * hnum);
    mnum = Math.floor(minutes);
    if (String(mnum).length == 1) {
        mnum = "0" + mnum;
    }
    seconds = (now - grt) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
    snum = Math.round(seconds);
    if (String(snum).length == 1) {
        snum = "0" + snum;
    }
    document.getElementById("timeDate").innerHTML = " 本站已各种夹缝中安全运行 " + dnum + " 天 ";
    document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
}
setInterval("createtime()", 250);
</script>

<!-- 添加网站运行时间》-->
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
